var express = require("express");
const multer = require("multer");
const path = require("path");
var router = express.Router();
const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();

router.post("/user/getinfo", async (req, res) => {
  const { type, user_name, page_num, page_size } = req.body;
  const total = await prisma.user_all.count();
  try {
    const userSelect = await prisma.user_all.findMany({
      where: {
        user_name: {
          contains: user_name,
        },
        user_role: type === "all" ? undefined : type === "admin" ? 1 : 0,
      },
    });
    const userinfo = await prisma.user_all.findMany({
      skip: (page_num - 1) * page_size,
      take: Number(page_size),
      where: {
        user_name: {
          contains: user_name,
        },
        user_role: type === "all" ? undefined : type === "admin" ? 1 : 0,
      },
    });
    result = {
      total: userSelect.length,
      page_num: page_num,
      page_size: page_size,
      info_size: userinfo.length,
      userinfo: userinfo,
    };
    res.json(result);
  } catch (error) {
    console.log(error);
  }
});

router.post("/user/update", async (req, res) => {
  const { user_name, phone, email, type } = req.body;

  try {
    const updateUser = await prisma.user_all.updateMany({
      where: {
        user_name: user_name,
      },
      data: {
        phone: phone,
        email: email,
        user_role: Number(type),
      },
    });
    if (updateUser) {
      result = {
        msg: 1,
        count: updateUser.count,
      };
      res.json(result);
    } else {
      res.json({ msg: 0 });
    }
  } catch (error) {
    res.json({ msg: 0 });
  }
});

router.get("/user/delete", async (req, res) => {
  const { user_id } = req.query;

  try {
    const deleteUser = await prisma.user_all.delete({
      where: {
        user_id: Number(user_id),
      },
    });
    if (deleteUser) {
      result = {
        msg: 1,
      };
      res.json(result);
    } else {
      res.json({ msg: 0 });
    }
  } catch (error) {
    res.json({ msg: 0 });
  }
});

router.post("/user/add", async (req, res) => {
  const { username, phone, email, type, password } = req.body;

  try {
    const addUser = await prisma.user_all.create({
      data: {
        user_name: username,
        phone: phone,
        email: email.length > 0 ? email : undefined,
        user_role: Number(type),
        user_pwd: password,
      },
    });
    if (addUser) {
      result = {
        msg: 1,
      };
      res.json(result);
    } else {
      res.json({ msg: 0 });
    }
  } catch (error) {
    res.json({ msg: 0 });
  }
});

// 配置文件上传
const storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "public/img/scenery");
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + path.extname(file.originalname));
  },
});
const upload = multer({ storage: storage });

router.post("/upload", upload.single("image"), function (req, res) {
  // 返回上传的文件 URL
  const imageUrl = "http://localhost:3000/img/scenery/" + req.file.filename;
  res.json({ url: imageUrl });
});

module.exports = router;
